import {
    CHANGE_SONG,
    CHANGE_CURRENT_SONG_INDEX,
    CHANGE_PLAYER_LIST,
    CHANGE_SEQUENCE,
    CHANGE_LYRIC,
    CHANGE_CURRENT_LYRIC_INDEX,
    CHANGE_ISBOTTOM_MUSIC,
    CHANGE_SIMI_PLAYLIST,
    CHANGE_SIMI_SONGS
} from "./constants"
import { getSongDetail, getLyric, getSimiPlaylist, getSimiSong } from "@/services/player"
import { parselrc } from "@/utils/parse-lrc"
const changeCurrentSong = (res) => ({
    type: CHANGE_SONG,
    currentSong: res
})
const changePlayListACtion = (songs) => ({
    type: CHANGE_PLAYER_LIST,
    playList: songs
})
const changeCurrentSongIndexCtion = (index) => ({
    type: CHANGE_CURRENT_SONG_INDEX,
    index: index
})

export const changeCurrentSongAction = (idx) => {

    return (dispatch, getState) => {

        const playList = getState().get("player").get("playList")
        const index = playList.findIndex((item) => item.id === idx)
        let song = null
        if (index !== -1) {
            song = playList[index]
            dispatch(changeCurrentSongIndexCtion(index))
            dispatch(changeCurrentSong(song))
            if (!song) return
            dispatch(getLyricAction(song.id))
        } else {
            getSongDetail(idx).then((res) => {
                // console.log(res)
                song = res.songs && res.songs[0]
                if (!song) return
                playList.push(song)
                const newPlayList = [...playList]
                dispatch(changePlayListACtion(newPlayList))
                dispatch(changeCurrentSongIndexCtion(newPlayList.length - 1))
                dispatch(changeCurrentSong(song))
                if (!song) return
                dispatch(getLyricAction(song.id))

            })
        }

    }
}

export const changeSequenceAction = (sequence) => ({
    type: CHANGE_SEQUENCE,
    sequence
})

export const changeCurrentSongsAction = (tag) => {

    return (dispatch, getState) => {
        const playList = getState().getIn(["player", "playList"])
        const sequence = getState().getIn(["player", "sequence"])
        let currentSongIndex = getState().getIn(["player", "currentSongIndex"])
        switch (sequence) {
            case 1:
                let currentIndex = Math.floor(Math.random() * playList.length)
                currentSongIndex = currentIndex
                break
            default:
                // eslint-disable-next-line no-unused-vars
                currentSongIndex += tag;
                if (currentSongIndex >= playList.length) currentSongIndex = 0
                if (currentSongIndex < 0) currentSongIndex = playList.length - 1

        }
        const song = playList[currentSongIndex]
        dispatch(changeCurrentSong(song))
        dispatch(changeCurrentSongIndexCtion(currentSongIndex))
        if (!song) return
        dispatch(getLyricAction(song.id))
    }
}

const changeLyricAction = (lyric) => ({
    type: CHANGE_LYRIC,
    lyric
})

export const getLyricAction = (id) => {

    return dispatch => {
        getLyric(id).then(res => {
            const musicLrc = parselrc(res.lrc.lyric)
            dispatch(changeLyricAction(musicLrc))
        })
    }
}

export const changeCurrentLyricIndexAction = (lyricIndex) => ({
    type: CHANGE_CURRENT_LYRIC_INDEX,
    lyricIndex
})



const changeSimiPlaylistAction = (res) => ({
    type: CHANGE_SIMI_PLAYLIST,
    simiPlaylist: res.playlists
})

const changeSimiSongsAction = (res) => ({
    type: CHANGE_SIMI_SONGS,
    simiSongs: res.songs
})

export const changeIsBottomMusicAction = (isBottomMusic) => ({
    type: CHANGE_ISBOTTOM_MUSIC,
    isBottomMusic
})

export const getSimiPlaylistAction = () => {
    return (dispatch, getState) => {
        const id = getState().getIn(["player", "currentSong"]).id;
        if (!id) return;

        getSimiPlaylist(id).then(res => {
            dispatch(changeSimiPlaylistAction(res));
        })
    }
}

export const getSimiSongAction = () => {
    return (dispatch, getState) => {
        const id = getState().getIn(["player", "currentSong"]).id;
        if (!id) return;

        getSimiSong(id).then(res => {
            dispatch(changeSimiSongsAction(res));
        })
    }
}